package com.kiln.juxingqs.ui.jy

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import client.global.commlib.base.BaseActivity
import client.global.commlib.dialog.ValidatePwdDialog
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.api.event.EventUserInfo
import com.kiln.juxingqs.databinding.ActivityHuazhuanBinding
import com.kiln.juxingqs.ui.vm.ZzViewModel
import org.greenrobot.eventbus.EventBus
import java.util.*


class HuaZhuanActivity : BaseActivity<ZzViewModel, ActivityHuazhuanBinding>() {

    var goods_id: String? = ""

    override fun getLayout(): Int {
        return R.layout.activity_huazhuan
    }

    override fun initView() {
        binding?.apply {
            event = this@HuaZhuanActivity
            vm = viewModel
            StoreUtil.getBean<UserInfo>(this@HuaZhuanActivity)?.let {
                viewModel.userInfo.value = it
                user = it
            }
        }

        goods_id = intent.getStringExtra("goods_id")
        var take_num = intent.getStringExtra("take_num")

    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    EventBus.getDefault().post(EventUserInfo())
                    startActivity(Intent(this@HuaZhuanActivity, TicketTransRecordActivity::class.java).apply {
                        putExtra("goods_id", goods_id)
                    })
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
    }

    override fun initViewModel(): ZzViewModel = createViewModel()

    fun ok() {
        if (goods_id.isNullOrEmpty()) {
            toast("goods_id 不能为空")
            return
        }
        if (viewModel.phone.value.isNullOrEmpty() || TextUtils.isEmpty(viewModel.txNum.value)) {
            toast("所有不能为空")
            return
        }
        ValidatePwdDialog(this).apply {
            title = "需要验证支付密码"
            ok = {
                viewModel.okHuaZhuan(goods_id!!, it)
            }
        }.show()
    }

    fun jl() {
        startActivity(Intent(this@HuaZhuanActivity, TicketTransRecordActivity::class.java).apply {
            putExtra("goods_id", goods_id)
        })
    }

}